package com.insigma.business.qrcode.appmgt.service.impl;

import com.insigma.business.qrcode.appmgt.dao.CareWelfareTotalDao;
import com.insigma.business.qrcode.appmgt.dto.request.CareWelfareTotalSearchRequest;
import com.insigma.business.qrcode.appmgt.dto.response.*;
import com.insigma.business.qrcode.appmgt.dto.search.CareWelfareTotalSearch;
import com.insigma.business.qrcode.appmgt.service.CareWelfareTotalService;
import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.BizException;
import com.insigma.business.qrcode.common.util.DateUtils;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("careWelfareTotalService")
public class CareWelfareTotalServiceImpl extends BaseProviderImpl implements CareWelfareTotalService {
    @Resource
    CareWelfareTotalDao careWelfareTotalDao;

    @Override
    public BaseResponse<CareWelfareTotalResponse> queryCareWelfareTotal(CareWelfareTotalSearchRequest searchRequest) {
        final BaseResponse<CareWelfareTotalResponse> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询的请求参数不能为空");
                BizCheckUtil.checkNotBlank(searchRequest.getAdminAreaCode(),"行政区划不能为空");
                if(StringUtils.isNotEmpty(searchRequest.getTotalMonth()) && !"01,02,03,04,05,06,07,08,09,10,11,12".contains(searchRequest.getTotalMonth())){
                    throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "月份不在范围内，范围【01,02,03,04,05,06,07,08,09,10,11,12】");
                }
            }

            @Override
            public void executeService() {
                CareWelfareTotalSearch careWelfareTotalSearch = new CareWelfareTotalSearch();
                BeanUtils.copyProperties(searchRequest,careWelfareTotalSearch);
                if(StringUtils.isNotEmpty(searchRequest.getTotalYear())){
                    if(StringUtils.isNotEmpty(searchRequest.getTotalMonth())){
                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + "01");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + DateUtils.date2Str(new Date(),"dd"));
                    }else{

                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + "0101");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + DateUtils.date2Str(new Date(),"MMdd"));
                    }
                }
                CareWelfareTotalResponse careWelfareTotalResponse = careWelfareTotalDao.queryCareWelfareTotal(careWelfareTotalSearch);
                ResponseUtils.buildSuccessResponse(baseResponse, careWelfareTotalResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_TOTAL;
            }
        });

        return baseResponse;
    }

    @Override
    public BaseResponse<List<CareWelfareTotalMerchantTypeResponse>> queryMerchantTypeTotal(CareWelfareTotalSearchRequest searchRequest) {
        final BaseResponse<List<CareWelfareTotalMerchantTypeResponse>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询的请求参数不能为空");
                BizCheckUtil.checkNotBlank(searchRequest.getAdminAreaCode(),"行政区划不能为空");
                if(StringUtils.isNotEmpty(searchRequest.getTotalMonth()) && !"01,02,03,04,05,06,07,08,09,10,11,12".contains(searchRequest.getTotalMonth())){
                    throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "月份不在范围内，范围【01,02,03,04,05,06,07,08,09,10,11,12】");
                }
            }

            @Override
            public void executeService() {
                CareWelfareTotalSearch careWelfareTotalSearch = new CareWelfareTotalSearch();
                BeanUtils.copyProperties(searchRequest,careWelfareTotalSearch);
                if(StringUtils.isNotEmpty(searchRequest.getTotalYear())){
                    if(StringUtils.isNotEmpty(searchRequest.getTotalMonth())){
                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + "01");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + DateUtils.date2Str(new Date(),"dd"));
                    }else{

                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + "0101");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + DateUtils.date2Str(new Date(),"MMdd"));
                    }
                }
                List<CareWelfareTotalMerchantTypeResponse> careWelfareTotalMerchantTypeList = careWelfareTotalDao.queryMerchantTypeTotal(careWelfareTotalSearch);
                long count = careWelfareTotalMerchantTypeList.stream().map(o -> o.getMerchantNum()).reduce((long)0,(a,b) ->{
                    return a + b;
                });

                for (CareWelfareTotalMerchantTypeResponse o : careWelfareTotalMerchantTypeList) {
                    //new BigDecimal(100).multiply(new BigDecimal(o.getMerchantNum())).divide(new BigDecimal(count),2, BigDecimal.ROUND_UP)
                    o.setMerchantPro(new BigDecimal(100).multiply(new BigDecimal(o.getMerchantNum())).divide(new BigDecimal(count),1, BigDecimal.ROUND_UP) + "%");
                }

                ResponseUtils.buildSuccessResponse(baseResponse, careWelfareTotalMerchantTypeList);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_TYPE_TOTAL;
            }
        });

        return baseResponse;
    }

    @Override
    public BaseResponse<List<CareWelfareTotalAreaMerchantResponse>> queryAreaMerchantTotal(CareWelfareTotalSearchRequest searchRequest) {
        final BaseResponse<List<CareWelfareTotalAreaMerchantResponse>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询的请求参数不能为空");
                BizCheckUtil.checkNotBlank(searchRequest.getAdminAreaCode(),"行政区划不能为空");
                if(StringUtils.isNotEmpty(searchRequest.getTotalMonth()) && !"01,02,03,04,05,06,07,08,09,10,11,12".contains(searchRequest.getTotalMonth())){
                    throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "月份不在范围内，范围【01,02,03,04,05,06,07,08,09,10,11,12】");
                }
            }

            @Override
            public void executeService() {
                CareWelfareTotalSearch careWelfareTotalSearch = new CareWelfareTotalSearch();
                BeanUtils.copyProperties(searchRequest,careWelfareTotalSearch);
                if(StringUtils.isNotEmpty(searchRequest.getTotalYear())){
                    if(StringUtils.isNotEmpty(searchRequest.getTotalMonth())){
                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + "01");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + DateUtils.date2Str(new Date(),"dd"));
                    }else{

                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + "0101");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + DateUtils.date2Str(new Date(),"MMdd"));
                    }
                }
                List<CareWelfareTotalAreaMerchantResponse> careWelfareTotalAreaMerchantList = careWelfareTotalDao.queryAreaMerchantTotal(careWelfareTotalSearch);
                long count = careWelfareTotalAreaMerchantList.stream().map(o -> o.getMerchantNum()).reduce((long)0,(a,b) ->{
                    return a + b;
                });

                for (CareWelfareTotalAreaMerchantResponse o : careWelfareTotalAreaMerchantList) {
                    //new BigDecimal(100).multiply(new BigDecimal(o.getMerchantNum())).divide(new BigDecimal(count),2, BigDecimal.ROUND_UP)
                    o.setMerchantPro(new BigDecimal(100).multiply(new BigDecimal(o.getMerchantNum())).divide(new BigDecimal(count),1, BigDecimal.ROUND_UP) + "%");
                }

                ResponseUtils.buildSuccessResponse(baseResponse, careWelfareTotalAreaMerchantList);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_AREA_MERCHANT_TOTAL;
            }
        });

        return baseResponse;
    }

    @Override
    public BaseResponse<List<CareWelfareTotalMerchantEvaluationResponse>> queryMerchantEvaluationTotal(CareWelfareTotalSearchRequest searchRequest) {
        final BaseResponse<List<CareWelfareTotalMerchantEvaluationResponse>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询的请求参数不能为空");
                BizCheckUtil.checkNotBlank(searchRequest.getAdminAreaCode(),"行政区划不能为空");
                if(StringUtils.isNotEmpty(searchRequest.getTotalMonth()) && !"01,02,03,04,05,06,07,08,09,10,11,12".contains(searchRequest.getTotalMonth())){
                    throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "月份不在范围内，范围【01,02,03,04,05,06,07,08,09,10,11,12】");
                }
            }

            @Override
            public void executeService() {
                final CareWelfareTotalSearch careWelfareTotalSearch = new CareWelfareTotalSearch();
                BeanUtils.copyProperties(searchRequest,careWelfareTotalSearch);
                if(StringUtils.isNotEmpty(searchRequest.getTotalYear())){
                    if(StringUtils.isNotEmpty(searchRequest.getTotalMonth())){
                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + "01");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + DateUtils.date2Str(new Date(),"dd"));
                    }else{

                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + "0101");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + DateUtils.date2Str(new Date(),"MMdd"));
                    }
                }
                List<CareWelfareTotalMerchantEvaluationResponse> careWelfareTotalMerchantEvaluationList = careWelfareTotalDao.queryMerchantEvaluationTotal(careWelfareTotalSearch);

                ResponseUtils.buildSuccessResponse(baseResponse, careWelfareTotalMerchantEvaluationList);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_EVALUATION_TOTAL;
            }
        });

        return baseResponse;
    }

    @Override
    public BaseResponse<List<CareWelfareTotalMerchantDiscountResponse>> queryMerchantDiscountTotal(CareWelfareTotalSearchRequest searchRequest) {
        final BaseResponse<List<CareWelfareTotalMerchantDiscountResponse>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询的请求参数不能为空");
                BizCheckUtil.checkNotBlank(searchRequest.getAdminAreaCode(),"行政区划不能为空");
                if(StringUtils.isNotEmpty(searchRequest.getTotalMonth()) && !"01,02,03,04,05,06,07,08,09,10,11,12".contains(searchRequest.getTotalMonth())){
                    throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "月份不在范围内，范围【01,02,03,04,05,06,07,08,09,10,11,12】");
                }
            }

            @Override
            public void executeService() {
                CareWelfareTotalSearch careWelfareTotalSearch = new CareWelfareTotalSearch();
                BeanUtils.copyProperties(searchRequest,careWelfareTotalSearch);
                if(StringUtils.isNotEmpty(searchRequest.getTotalYear())){
                    if(StringUtils.isNotEmpty(searchRequest.getTotalMonth())){
                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + "01");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + DateUtils.date2Str(new Date(),"dd"));
                    }else{

                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + "0101");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + DateUtils.date2Str(new Date(),"MMdd"));
                    }
                }
                List<CareWelfareTotalMerchantDiscountResponse> careWelfareTotalMerchantDiscountList = careWelfareTotalDao.queryMerchantDiscountTotal(careWelfareTotalSearch);
                long count = careWelfareTotalMerchantDiscountList.stream().map(o -> o.getMerchantNum()).reduce((long)0,(a,b) ->{
                    return a + b;
                });

                for (CareWelfareTotalMerchantDiscountResponse o : careWelfareTotalMerchantDiscountList) {
                    //new BigDecimal(100).multiply(new BigDecimal(o.getMerchantNum())).divide(new BigDecimal(count),2, BigDecimal.ROUND_UP)
                    o.setMerchantPro(new BigDecimal(100).multiply(new BigDecimal(o.getMerchantNum())).divide(new BigDecimal(count),1, BigDecimal.ROUND_UP) + "%");
                }

                ResponseUtils.buildSuccessResponse(baseResponse, careWelfareTotalMerchantDiscountList);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_DISCOUNT_TOTAL;
            }
        });

        return baseResponse;
    }

    @Override
    public BaseResponse<List<CareWelfareTotalAreaMerchantDiscountResponse>> queryAreaMerchantDiscountTotal(CareWelfareTotalSearchRequest searchRequest) {
        final BaseResponse<List<CareWelfareTotalAreaMerchantDiscountResponse>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询的请求参数不能为空");
                BizCheckUtil.checkNotBlank(searchRequest.getAdminAreaCode(),"行政区划不能为空");
                if(StringUtils.isNotEmpty(searchRequest.getTotalMonth()) && !"01,02,03,04,05,06,07,08,09,10,11,12".contains(searchRequest.getTotalMonth())){
                    throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "月份不在范围内，范围【01,02,03,04,05,06,07,08,09,10,11,12】");
                }
            }

            @Override
            public void executeService() {
                final CareWelfareTotalSearch careWelfareTotalSearch = new CareWelfareTotalSearch();
                BeanUtils.copyProperties(searchRequest,careWelfareTotalSearch);
                if(StringUtils.isNotEmpty(searchRequest.getTotalYear())){
                    if(StringUtils.isNotEmpty(searchRequest.getTotalMonth())){
                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + "01");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + DateUtils.date2Str(new Date(),"dd"));
                    }else{

                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + "0101");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + DateUtils.date2Str(new Date(),"MMdd"));
                    }
                }
                List<CareWelfareTotalAreaMerchantDiscountResponse> careWelfareTotalAreaMerchantDiscountList = careWelfareTotalDao.queryAreaMerchantDiscountTotal(careWelfareTotalSearch);

                ResponseUtils.buildSuccessResponse(baseResponse, careWelfareTotalAreaMerchantDiscountList);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_AREA_MERCHANT_DISCOUNT_TOTAL;
            }
        });

        return baseResponse;
    }

    @Override
    public BaseResponse<List<CareWelfareTotalMerchantDiscountChartResponse>> queryMerchantDiscountChartTotal(CareWelfareTotalSearchRequest searchRequest) {
        final BaseResponse<List<CareWelfareTotalMerchantDiscountChartResponse>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询的请求参数不能为空");
                BizCheckUtil.checkNotBlank(searchRequest.getAdminAreaCode(),"行政区划不能为空");
                if(StringUtils.isNotEmpty(searchRequest.getTotalMonth()) && !"01,02,03,04,05,06,07,08,09,10,11,12".contains(searchRequest.getTotalMonth())){
                    throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "月份不在范围内，范围【01,02,03,04,05,06,07,08,09,10,11,12】");
                }
            }

            @Override
            public void executeService() {
                final CareWelfareTotalSearch careWelfareTotalSearch = new CareWelfareTotalSearch();
                BeanUtils.copyProperties(searchRequest,careWelfareTotalSearch);
                if(StringUtils.isNotEmpty(searchRequest.getTotalYear())){
                    if(StringUtils.isNotEmpty(searchRequest.getTotalMonth())){
                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + "01");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + searchRequest.getTotalMonth() + DateUtils.date2Str(new Date(),"dd"));
                    }else{

                        careWelfareTotalSearch.setBeginDate(searchRequest.getTotalYear() + "0101");
                        careWelfareTotalSearch.setEndDate(searchRequest.getTotalYear() + DateUtils.date2Str(new Date(),"MMdd"));
                    }
                }
                List<CareWelfareTotalMerchantDiscountChartResponse> careWelfareTotalMerchantDiscountChartList = careWelfareTotalDao.queryMerchantDiscountChartTotal(careWelfareTotalSearch);

                ResponseUtils.buildSuccessResponse(baseResponse, careWelfareTotalMerchantDiscountChartList);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_DISCOUNT_CHART_TOTAL;
            }
        });

        return baseResponse;
    }
}
